﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DMS.DAL;
using DMS.Logic;
namespace WebUI.folders
{
    public partial class list : System.Web.UI.Page
    {
        private int selectedLib
        {
            get { if (drpLibrary.SelectedItem != null)return int.Parse(drpLibrary.SelectedValue); else return 0; }
        }
        private int selectedFolder
        {
            get { if (drpFolder.SelectedItem != null) return int.Parse(drpFolder.SelectedValue); else return 0; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadAll();
            }
        }
        void loadAll()
        {
            loadDrpLibrary();
            loadDrpFolder();
            loadGV();
        }
        void loadDrpLibrary()
        {
            drpLibrary.DataSource = DMS.Logic.BL.getInstance().getAllDocumentLibrary();
            drpLibrary.DataValueField = "ID";
            drpLibrary.DataTextField = "Name";
            drpLibrary.DataBind();
        }
        void loadDrpFolder()
        {
            drpFolder.Items.Clear();
            List<Folder> fldrs = DMS.Logic.BL.getInstance().getFoldersByLibrary(selectedLib,-1);
            drpFolder.DataSource = fldrs;
            drpFolder.DataValueField = "ID";
            drpFolder.DataTextField = "Name";
            drpFolder.DataBind();
            drpFolder.Items.Insert(0, new ListItem("", "0"));
        }
        void loadGV()
        {
            List<Folder> fldrs = BL.getInstance().getFoldersByLibraryAndFolder(selectedLib, selectedFolder, -1);
            if (fldrs == null)
            {
                fldrs = new List<Folder>();
                fldrs.Add(new Folder());
            }
            GV.DataSource = fldrs;
            GV.DataBind();
        }

        protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "add")
            {
                TextBox txtName = (TextBox)GV.FooterRow.FindControl("txtNamefooter");
                Folder fldr = new Folder();
                fldr.Name = txtName.Text;
                fldr.CreationDate = DateTime.Now;
                BL.getInstance().saveFolder(fldr, selectedLib,selectedFolder,1);
                loadAll();
            }
        }

        protected void drpLibrary_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadDrpFolder();
            GV.EditIndex = -1;
            GV.ShowFooter = true;
            loadGV();
        }

        protected void drpFolder_SelectedIndexChanged(object sender, EventArgs e)
        {
            GV.EditIndex = -1;
            GV.ShowFooter = true;
            loadGV();
        }

        protected void GV_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GV.ShowFooter = false;
            GV.EditIndex = e.NewEditIndex;
            loadGV();
        }

        protected void GV_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GV.EditIndex = -1;
            GV.ShowFooter = true;
            loadGV();
        }
        void loadInnerEditFolder(ref DropDownList drp,int current,string selectedValue)
        {
            List<Folder> fldrs = DMS.Logic.BL.getInstance().getFoldersByLibrary(selectedLib, current);
            drp.DataSource = fldrs;
            drp.DataValueField = "ID";
            drp.DataTextField = "Name";
            drp.DataBind();
            drp.Items.Insert(0, new ListItem("", "0"));
            drp.SelectedValue = selectedValue;
        }
        protected void GV_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && ((e.Row.RowState != (DataControlRowState.Alternate | DataControlRowState.Edit) && (e.Row.RowState != (DataControlRowState.Normal | DataControlRowState.Edit)) && (e.Row.RowState != (DataControlRowState.Alternate | DataControlRowState.Insert) && (e.Row.RowState != (DataControlRowState.Normal | DataControlRowState.Insert))))))
            {
                Label lblParentFolder = (Label)e.Row.FindControl("lblParentFolder");
                lblParentFolder.Text = "N/A";
                Folder pf = (Folder)DataBinder.Eval(e.Row.DataItem, "Folder2");
                string name = (string)DataBinder.Eval(e.Row.DataItem, "name");
                if (string.IsNullOrEmpty(name))
                    e.Row.Visible = false;
                if (pf != null)
                    lblParentFolder.Text = pf.Name;

            }
            else if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState != (DataControlRowState.Alternate | DataControlRowState.Edit) || (e.Row.RowState != (DataControlRowState.Normal | DataControlRowState.Edit))))
            {
                int current = (int)GV.DataKeys[e.Row.RowIndex].Value;
                DropDownList drp = (DropDownList)e.Row.FindControl("drpFolder");
                string sv = "0";
                Folder pf = (Folder)DataBinder.Eval(e.Row.DataItem, "Folder2");
                if (pf != null)
                    sv = pf.ID.ToString();
                loadInnerEditFolder(ref drp, current, sv);

            }
        }

        protected void GV_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int intFldr=0;
            TextBox txtName = (TextBox)GV.Rows[e.RowIndex].FindControl("txtName");
            int id = (int)GV.DataKeys[e.RowIndex].Value;
            Folder fldr = BL.getInstance().getFolderById(id);
            fldr.Name = txtName.Text;
            DropDownList drp = (DropDownList)GV.Rows[e.RowIndex].FindControl("drpFolder");
            if (drp.SelectedValue != null)
                intFldr = int.Parse(drp.SelectedValue);
            BL.getInstance().updateFolder(fldr, selectedLib, intFldr, 1);
            cancel();
        }

        protected void GV_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int fldrId = 0;
            fldrId = (int)GV.DataKeys[e.RowIndex].Value;
            BL.getInstance().deleteFolder(fldrId);
            cancel();
        }
        void cancel()
        {
            GV.EditIndex = -1;
            GV.ShowFooter = true;
            loadAll();
        }
    }
}
